﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Common.XmlConfig;

namespace ZWZ.Web.WebAdmin
{
    public partial class bd : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }
        private void BindData()
        {
            var CurrentUser = Codes.LoginHelper.CurrentUser;
            var loginInfo = DBProvider.DB.Ctx.User_Login.Where(i => i.UID == CurrentUser.ID).OrderBy(i => i.LoginTime).Take(2).ToList();
            if (loginInfo.Count > 1)
            {
                lab_lastlogin.Text = loginInfo[1].LoginTime.ToString("yyyy-MM-dd HH:mm:ss");
                lab_lastloginIP.Text = loginInfo[1].IP;
            }
            if (loginInfo.Count > 0)
            {
                lab_curlogin.Text = loginInfo[0].LoginTime.ToString("yyyy-MM-dd HH:mm:ss");
                lab_curloginIP.Text = loginInfo[0].IP;
            }
            lab_Birthday.Text = CurrentUser.Birthday.ToString("yyyy年MM月dd日");
            lab_CreateDate.Text = CurrentUser.CreateDate.ToString("yyyy-MM-dd HH:mm:ss");
            lab_Email.Text = CurrentUser.Email;
            lab_Introduction.Text = string.IsNullOrWhiteSpace(CurrentUser.Introduction) ? "未填写" : CurrentUser.Introduction;
            lab_Name.Text = CurrentUser.GroupName;
            lab_NickName.Text = CurrentUser.NickName;
            Image1.AlternateText = CurrentUser.NickName;
            Image1.ImageUrl = CurrentUser.PicUrl;
            RptSetting.DataSource = ZWZ.Web.Config.ConfigAdapter.SiteSettingMgr.Groups["WebSiteInfo"].Settings;
            RptSetting.DataBind();

            string 已发送未读 = ZWZ.Enums.User.MessageStatus.已发送未读.ToString();
            string 未发送草稿 = ZWZ.Enums.User.MessageStatus.未发送草稿.ToString();
            string 公告 = ZWZ.Enums.User.MessageType.公告.ToString();
            string 私信 = ZWZ.Enums.User.MessageType.私信.ToString();
            string 提问 = ZWZ.Enums.User.MessageType.提问.ToString();
            string CurrentUserID = Codes.LoginHelper.CurrentUser.ID;
            var MsgGroups = DBProvider.DB.Ctx.User_Message
                .Where(i => i.ReciverID == CurrentUserID)
                .GroupBy(i => new { i.Type, i.Status })
                .Select(arg => new
            {
                MsgType = arg.Key.Type,
                MsgStatus = arg.Key.Status,
                Count = arg.Count()
            }).ToList();

            var 未读私信 = MsgGroups.Where(i => i.MsgType == 私信 && i.MsgStatus == 已发送未读).Sum(i => i.Count);
            var 所有私信 = MsgGroups.Where(i => i.MsgType == 私信 && i.MsgStatus != 未发送草稿).Sum(i => i.Count);
            lab_msg.Text = 未读私信 + "/" + 所有私信;

            var 未读公告 = MsgGroups.Where(i => i.MsgType == 公告 && i.MsgStatus == 已发送未读).Sum(i => i.Count);
            var 所有公告 = MsgGroups.Where(i => i.MsgType == 公告 && i.MsgStatus != 未发送草稿).Sum(i => i.Count);
            lab_note.Text = 未读公告 + "/" + 所有公告;

            var 未读提问 = MsgGroups.Where(i => i.MsgType == 提问 && i.MsgStatus == 已发送未读).Sum(i => i.Count);
            var 所有提问 = MsgGroups.Where(i => i.MsgType == 提问 && i.MsgStatus != 未发送草稿).Sum(i => i.Count);
            lab_ask.Text = 未读提问 + "/" + 所有提问;
        }
    }
}